package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Myuser;

import ejb.HelperFunction;

import ejb.UserFacadeEJBRemote;

/**
 * Servlet implementation class Signup
 */
@WebServlet("/Signup")
public class Signup extends HttpServlet {
	private static final long serialVersionUID = 1L;
       @EJB UserFacadeEJBRemote uf; 
       //HelperFunction hp;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Signup() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.sendRedirect("home.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        
        String username=request.getParameter("username");
        //username=hp.TransSQLInject(username);
        String password=request.getParameter("password");
        //password=hp.TransSQLInject(password);
       
        //Myuser user=new Myuser(); 
        boolean ine=uf.isNameExist(username);
        String err1="*Username exist, please change.";
        
        if(ine){
            request.setAttribute("error",err1);
            request.setAttribute("name",username);
            request.getRequestDispatcher("signup.jsp").forward(request, response);
        }
        else{
            uf.createUser(username,password);
            out.print("Success! redirecting to Login in 3s...");
            response.setHeader("Refresh","3;URL=login.jsp");
        }
	}

}
